<?php
namespace app\site\controller;

use app\common\model\AuthGroupModel;
use app\common\model\UserMemberModel;
use app\common\model\UserModel;
use think\Db;
use think\Request;

class UserController extends BaseController
{

    private $userModel;
    private $userMemberModel;
    private $authGroupModel;

    /**
     * 初始化
     */
    public function __construct(Request $request)
    {
        parent::__construct($request);
        $this->userModel      = new UserModel();
        $this->userMemberModel= new UserMemberModel();
        $this->authGroupModel = new AuthGroupModel();
    }

    /**
     * 会员首页
     * @return mixed
     */
    public function index()
    {
        $map          = [];
        $query        = [];
        $searchKey    = $this->request->param('searchKey');
        $searchText   = $this->request->param('searchText');
        if (!empty($searchKey) && !empty($searchText)) {
            $map[$searchKey]     = ['like', '%' . $searchText . '%'];
            $query['searchKey']  = $searchKey;
            $query['searchText'] = $searchText;
        }
        $fields      = [
            'User'        => 'uid,username,name,mobile,email,status,create_time,login_times,last_login_ip,last_login_time',
            'user_member' => 'openid,alias,sex,headimgurl,birthday,user_money,prepare_money,pay_points,prepare_points,parent_id',
        ];
        $model       = Db::view('user', $fields['User'])
            ->view('user_member', $fields['user_member'], 'user_member' . '.uid=user.uid');
        $list  = $model->where($map)->where('site_id>=0')->order('uid desc')->paginate(12, false, ['query' => $query]);
        $this->assign('list', $list);
        return $this->fetch();
    }

    /**
     * 添加用户
     */
    public function add()
    {
        if ($this->request->isAjax()) {
            $data   = $this->request->param();
            $data['site_id'] = 0;
            $result = $this->validate($data, 'UserMemberValidate.add');
            if (true !== $result) {
                $this->error($result);
            }
            if ($this->userModel->addData($data)) {
                $this->success('添加成功！', url('User/index'));
            } else {
                $this->error($this->userModel->getError());
            }
        }
        $groupList = $this->authGroupModel->where('utype', 'member')->order('group_sort asc')->select();
        $this->assign('groupList', $groupList);
        $this->assign('utype',$this->getUtype());
        return $this->fetch();
    }

    /**
     * 修改数据
     * @return mixed
     */
    public function edit()
    {
        if ($this->request->isAjax()) {
            $data   = $this->request->param();
            if ($this->userModel->editData($data)) {
                $this->success('修改成功！', url('User/index'));
            } else {
                $this->error($this->userModel->getError());
            }
        }
        $uid       = $this->request->param('uid');
        $fields      = [
            'User'        => 'uid,name,password,username,mobile,email,status,group_id,create_time,login_times,last_login_ip,last_login_time,last_login_ip,last_login_time',
            'user_member' => 'openid,alias,sex,headimgurl,birthday,user_money,prepare_money,pay_points,prepare_points,parent_id',
        ];
        $model       = Db::view('user', $fields['User'])
            ->view('user_member', $fields['user_member'], 'user_member' . '.uid=user.uid');
        $info  = $model->where('uid',$uid)->find();
        $groupList = $this->authGroupModel->where('utype', 'member')->order('group_sort asc')->select();
        $this->assign('groupList', $groupList);
        $this->assign('info', $info);
        $this->assign('utype',$this->getUtype());
        return $this->fetch();
    }

    /**
     * 修改密码
     */
    public function editpass()
    {
        if ($this->request->isAjax()) {
            $oldPassword     = $this->request->param('old_password');
            $newPassword     = $this->request->param('new_password');
            $replayPassword  = $this->request->param('replay_password');
            $map['uid']      = $this->getUid();
            $map['password'] = md5($oldPassword);
            if ($this->userModel->where($map)->find()) {
                if ($newPassword == $replayPassword) {
                    $data['uid']      = $this->getUid();
                    $data['password'] = $newPassword;
                    $this->userModel->editData($data);
                    cookie('systemDes', null);
                    $this->success('密码修改成功,请重新登陆！', url('editpass'));
                } else {
                    $this->error('原密码和确认密码不一致！');
                }
            } else {
                $this->error('原密码输入错误！');
            }

        }
        $info = $this->userModel->get($this->getUid());
        $this->assign('info', $info);
        return $this->fetch();
    }

    /**
     * 删除数据
     */
    public function del()
    {
        $uid = $this->request->param('uid', 0);
        $status = $this->request->param('status', 0);
        if ($this->userModel->where('uid',$uid)->setField('status',-$status+1)) {
            $this->success('操作成功！');
        } else {
            $this->error($this->userModel->getError());
        }
    }

}
